//const api = require('../../../services/config/api')
import { requestlogin } from '../../util'
const api = require('../../api')
const filter = require('../../utils/filter')

// pages/login/index.js
Page(
  filter.loginCheck({
    /**
     * 页面的初始数据
     */
    data: {},

    async wxLogin(e) {
      var that = this
      wx.showModal({
        //手动授权弹窗
        title: '授权登录提示',
        content: '是否允许授权？',
        showCancel: true,
        confirmText: '允许',
        confirmColor: '#669F76',
        cancelColor: '#CC463D',
        success: async res => {
          if (res.confirm) {
            // 自定义加载图标
            // Toast.loading({
            //   message: '加载中...',
            //   forbidClick: true,
            //   loadingType: 'spinner',
            //   duration: 0
            // })
            wx.login({
              success: res => {
                console.log(res)
                var obj = {}
                if (res.code) {
                  wx.getUserInfo({
                    success: function (elm) {
                      console.log(elm)
                      obj['code'] = res.code
                      obj['userInfo'] = elm.userInfo
                      obj['iv'] = elm.iv
                      obj['encryptedData'] = elm.encryptedData
                      that.doLogin(obj)
                    }
                  })
                } else {
                  console.log('获取用户登录态失败！' + res.errMsg)
                }
              }
            })
          } else if (res.cancel) {
            //用户取消
            wx.showToast({
              title: '取消授权',
              mask: true,
              icon: 'error'
            })
          }
        }
      })
    },

    doLogin: function (userInfo) {
      var that = this
      console.log(userInfo)
      requestlogin(api.AuthLoginByWeixin, userInfo, 'post').then(function (res) {
        console.log(res)
        if (res.code == 0) {
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 2000
          })
           //存储用户信息
           wx.setStorageSync('token', res.data.token)
           wx.setStorageSync('userInfo', res.data.userInfo)
           wx.setStorageSync('bindphone', false)
          if(res.data.bindphone){
            //未绑定手机号，跳转到绑定手机号
            wx.setStorageSync('bindphone',true)
            setTimeout(() => {
              wx.navigateTo({ url: '/pages/login/bindcode/index' })
            }, 1000);
          }
          //跳转到个人信息页，绑定手机号
          //wx.switchTab({ url: '/pages/user-center/index?currAuthStep=2' })
          
        }
      })
    },

    accountLogin() {
      wx.navigateTo({
        url: '/pages/login/account/index'
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
  })
)
